﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace SocketLibrary
{
    public class CommandInfo
    {
        
        private CommandType m_command_type;

        /// <summary>
        /// 命令类型
        /// </summary>
        public CommandType CommandType
        {
            get { return m_command_type; }
            set { m_command_type = value; }
        }

        private IPAddress m_sender_ip;

        /// <summary>
        /// 发送者IP
        /// </summary>
        public IPAddress SenderIP
        {
            get { return m_sender_ip; }
            set { m_sender_ip = value; }
        }

        private string m_sender_name;

        /// <summary>
        /// 发送者机器名称
        /// </summary>
        public string SenderName
        {
            get { return m_sender_name; }
            set { m_sender_name = value; }
        }

        private string m_data_sign;

        /// <summary>
        /// 数据标识
        /// </summary>
        public string DataSign
        {
            get { return m_data_sign; }
            set { m_data_sign = value; }
        }
	
   

        private byte[] m_send_data;

        /// <summary>
        /// 发送的数据
        /// </summary>
        public byte[] SendData
        {
            get { return m_send_data; }
            set { m_send_data = value; }
        }

        private string m_ex_data="none";
        /// <summary>
        /// 异常数据
        /// </summary>
        public string ExData
        {
            get { return m_ex_data; }
            set { m_ex_data= value; }
        }
	

        public CommandInfo()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="i_command_type"></param>
        /// <param name="i_sender_ip"></param>
        /// <param name="i_sender_name"></param>
        /// <param name="i_send_data"></param>
        public CommandInfo(CommandType i_command_type, IPAddress i_sender_ip, string i_sender_name, byte[] i_send_data)
        {
            this.m_command_type = i_command_type;
            this.m_sender_ip = i_sender_ip;
            this.m_sender_name = i_sender_name;
            this.m_send_data = i_send_data;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="i_command_type"></param>
        /// <param name="i_sender_ip"></param>
        /// <param name="i_send_data"></param>
        public CommandInfo(CommandType i_command_type, IPAddress i_sender_ip, byte[] i_send_data)
        {
            this.m_command_type = i_command_type;
            this.m_sender_ip = i_sender_ip;
            this.m_send_data = i_send_data;
        }

        /// <summary>
        /// 获取数据 字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (m_send_data != null && m_send_data.Length > 0)
                return Encoding.UTF8.GetString(m_send_data);

            return string.Empty;
        }

        /// <summary>
        /// 设置要发送的数据
        /// </summary>
        /// <param name="i_data"></param>
        public void SetSendData(string i_data)
        {
            m_send_data = Encoding.UTF8.GetBytes(i_data);
            m_data_sign = i_data;
        }

        /// <summary>
        /// 设置要发送的数据
        /// </summary>
        /// <param name="i_data"></param>
        public void SetSendData(int i_data)
        {
            m_send_data = BitConverter.GetBytes(i_data);
        }

        /// <summary>
        /// 设置要发送的数据
        /// </summary>
        /// <param name="i_data"></param>
        public void SetSendData(DataSet i_data)
        {
            if (i_data == null)
                return;
            i_data.RemotingFormat = SerializationFormat.Binary; //指定DataSet串行化格式是二进制

            MemoryStream t_ms = new MemoryStream();//定义内存流对象，用来存放DataSet序列化后的值

            IFormatter t_iformater = new BinaryFormatter();//产生二进制序列化格式

            t_iformater.Serialize(t_ms, i_data);//串行化到内存中

            m_send_data = t_ms.ToArray(); // 将DataSet转化成byte[]

            t_ms.Close();

            t_ms.Dispose();

        }

        /// <summary>
        /// 构造int
        /// </summary>
        /// <returns></returns>
        public int ToInt()
        {
            if (m_send_data == null || m_send_data.Length <= 0)
                return -1;

            return BitConverter.ToInt32(m_send_data, 0);
        }

        /// <summary>
        /// 获取数据 dataset
        /// </summary>
        /// <returns></returns>
        public DataSet ToDataSet()
        {
            if (m_send_data == null)
                return null;
            try
            {
                MemoryStream t_ms = new MemoryStream(m_send_data);//创建内存流

                IFormatter t_bf = new BinaryFormatter();//产生二进制序列化格式

                object t_obj = t_bf.Deserialize(t_ms);//反串行化到内存中

                //类型检验

                if (t_obj is DataSet)
                {
                    DataSet t_ds = (DataSet)t_obj;

                    return t_ds;
                }
                else
                {
                    return null;
                }

                t_ms.Close();

                return null;
            }
            catch
            {
                return null;
            }
        }

       
	
    }
}
